+function ($) {
    var ExtValidate = function (f, e) {
        this.$element =  $(f);
        this.options = $.extend({}, ExtValidate.DEFAULTS, e);
        this.init()
    };
    ExtValidate.DEFAULTS={

    }
    ExtValidate.prototype.init = function () {
        var $form = this.$element,
            options = $form.data().options,
            options =  (options && typeof options === 'string') ?options.toObj():options,

            $els = $form.find(":input[type='text'], :input[type='password']," +
                " :input[type='radio'], :input[type='checkbox'], :input[type='file'],:input[type='hidden'], select , textarea");
        $els.each(function () {
            var $el = $(this);
            if ($el.attr("data-rule-required")) {
                var $formGroup = $el.parent().prev(".bolt-form-label");
                if ($formGroup.find("span.required").length == 0) {
                    $formGroup.append('<span class="required">*</span>')
                }
            }
        });
        options = $.extend(options,{
            errorElement: "span",
            errorClass: "help-block",
            focusInvalid: true,
            ignore: ".note-editor *",
            errorPlacement: function (error, element) {

                if (element.parent(".input-append").size() > 0) {
                    error.insertAfter(element.parent(".input-append"))
                } else {
                    error.insertAfter(element)
                }
            },
            highlight: function (element) { // hightlight error inputs
                // $(element).closest('.help-inline').removeClass('ok'); // display OK icon
                $(element).closest('.bolt-form-item').removeClass('success').addClass('has-error'); // set error class to the control group
            },
            unhighlight: function (element) { // revert the change dony by hightlight
                $(element).closest('.bolt-form-item').removeClass('has-error'); // set error class to the control group
            }
        })
        $form.validate(options);
    }
    function Plugin(e) {
        Bolt.assert($.fn.validate, "依赖组件 validate 未引入");
        return this.each(function () {
            var h = $(this);
            var g = h.data("extValidate");
            var f = typeof e == "object" && e;
            if (!g) {
                h.data("extValidate", (g = new ExtValidate(this, f)))
            }
            if (typeof e == "string") {
                g[e]()
            }
        })
    }
    $.fn.extValidate = Plugin
    $.fn.extValidate.Constructor = ExtValidate;

    Bolt.addComponent({
        name: "ExtValidate",
        plugin: Plugin,
        runPoint: Bolt.eventType.afterAjaxPageShow,
        expr: "form.form-validation",
        order: 800
    })
}(jQuery);